﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BuildingDrawer
{
    class E_ElementFrame
    {
        #region variable

        public float m_timestamp;
        public List<E_Element> m_element_added;
        public List<E_Element> m_element_removed;
        public List<E_Element> m_element_changed;
        public Hashtable m_element_current;

        #endregion


        #region function
        public E_ElementFrame(float in_time)
        {
            m_timestamp = in_time;
            m_element_added = new List<E_Element>();
            m_element_removed = new List<E_Element>();
            m_element_changed = new List<E_Element>();
            m_element_current = new Hashtable();
        }

        public E_ElementFrame(float in_time,E_LineFrame in_frame)
        {
            m_timestamp = in_time;
            m_element_current = new Hashtable(in_frame.m_line_current);
        }

        public float getTime() { return m_timestamp; }

        public void AddElement(E_Element l)
        {
            m_element_current.Add(l.id, l);
        }

        public void RemoveLine(E_Element l)
        {
            m_element_current.Remove(l.id);
        }

        public void ChangeLine(E_Element l)
        {
            m_element_current.Remove(l.id);
            m_element_current.Add(l.id, l);
        }

        // id p1id p2id all the same
        private Boolean isContainElement(E_Element l)
        {
            foreach (DictionaryEntry de in m_element_current)
            {
                E_Element m_l = (E_Element)de.Value;
                if (l.isEqual(m_l))
                {
                    return true;
                }
            }
            return false;
        }

         //id is same is ok
        private Boolean isContainElementID(E_Element l)
        {
            if (m_element_current.ContainsKey(l.id))
            {
                return true;
            }
            return false;
        }

        public void compareElementFrame(E_ElementFrame last_frame)
        {
            foreach (DictionaryEntry de in last_frame.m_element_current)
            {
                E_Element l = (E_Element)de.Value;

                if (!this.isContainElementID(l))
                {
                    this.m_element_removed.Add(l);//////////////
                }
            }

            foreach (DictionaryEntry de in this.m_element_current)
            {
                E_Element m_l = (E_Element)de.Value;

                if (!last_frame.isContainElement(m_l)) // last frame does not contain the same line
                {
                    if (!last_frame.isContainElementID(m_l))
                    {
                        this.m_element_added.Add(m_l);
                    }
                    else
                    {
                        this.m_element_changed.Add(m_l);
                    }
                }
            }
        }
        
        #endregion
    }
}
